﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Trirand.Web.Mvc;

namespace Catalyst.Mvc
{
	public partial class LiteGrid<TGrid>
	{
		public void EditAsTextBox(params string[] dataFields)
		{
			if (AjaxCallBackMode == AjaxCallBackMode.RequestData)
			{
				foreach (string dataField in dataFields)
				{
					JQGridColumn column = getCustomEditColumn(dataField);
					column.EditType = EditType.TextBox;
				}
			}
		}

		public void EditAsTextArea(params string[] dataFields)
		{
			if (AjaxCallBackMode == AjaxCallBackMode.RequestData)
			{
				foreach (string dataField in dataFields)
				{
					JQGridColumn column = getCustomEditColumn(dataField);
					column.EditType = EditType.TextArea;
				}
			}
		}

		public void EditAsCheckBox(params string[] dataFields)
		{
			if (AjaxCallBackMode == AjaxCallBackMode.RequestData)
			{
				foreach (string dataField in dataFields)
				{
					JQGridColumn column = getCustomEditColumn(dataField);
					column.EditType = EditType.CheckBox;
				}
			}
		}

		public void EditAsPassword(params string[] dataFields)
		{
			if (AjaxCallBackMode == AjaxCallBackMode.RequestData)
			{
				foreach (string dataField in dataFields)
				{
					JQGridColumn column = getCustomEditColumn(dataField);
					column.EditType = EditType.Password;
				}
			}
		}

		public void EditAsDropDown(string dataField, IEnumerable<SelectListItem> editList)
		{
			EditAsDropDown(dataField, editList, false, null);
		}

		public void EditAsDropDown(string dataField, IEnumerable<SelectListItem> editList, bool sort)
		{
			EditAsDropDown(dataField, editList, sort, null);
		}

		public void EditAsDropDown(string dataField, IEnumerable<SelectListItem> editList, string heading)
		{
			EditAsDropDown(dataField, editList, false, null);
		}

		public void EditAsDropDown(string dataField, IEnumerable<SelectListItem> editList, bool sort, string heading)
		{
			JQGridColumn column = getComboEditColumn(dataField, editList, sort, heading);
			column.EditType = EditType.DropDown;
		}

		private JQGridColumn getComboEditColumn(string dataField, IEnumerable<SelectListItem> editList, bool sort, string heading)
		{
			JQGridColumn column = null;

			//if (AjaxCallBackMode == AjaxCallBackMode.RequestData)
			//{
				column = getCustomEditColumn(dataField);

				column.EditList = new List<SelectListItem>();

				if (heading != null)
				{
					column.EditList.Add(new SelectListItem
					{
						Text = heading,
						Value = "-1"
					});
				}

				if (editList != null)
				{
					List<SelectListItem> list = new List<SelectListItem>(editList);

					if (sort)
					{
						list.Sort(new Comparison<SelectListItem>(delegate(SelectListItem x, SelectListItem y)
						{
							return x.Text.CompareTo(y.Text);
						}));
					}

					column.EditList.AddRange(list);
				}
			//}

			return column;
		}

		private JQGridColumn getCustomEditColumn(string dataField)
		{
			JQGridColumn column = Columns.Find(c => c.DataField == dataField);
			column.Editable = true;

			return column;
		}
	}
}
